package com.shut.vehicle.controller.result;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.shut.vehicle.util.StrParamUtil;
import io.netty.util.internal.StringUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author 卢斌
 */
@Getter
@Setter
@NoArgsConstructor
public class Result implements Serializable {

    Integer code;

    Object data;

    String message = StringUtil.EMPTY_STRING;


    public static Result ok() {
        Result result = new Result();
        result.setCode(1);
        return result;
    }


    public static Result ok(Object s) {
        Result result = new Result();
        result.setCode(1);
        result.setData(s);
        return result;
    }

    public static Result okList(List<?> data) {
        PageResult pageResult = new PageResult(data);
        Result result = new Result();
        result.setCode(1);
        result.setData(pageResult);
        return result;
    }

    public static Result okList(List<? extends Serializable> dataList, JSONObject model) {
        PageResult pageResult = new PageResult(dataList, model);
        Result result = new Result();
        result.setCode(1);
        result.setData(pageResult);
        return result;
    }

    public static Result okList(List<?> data, Integer pageNum, Integer pageSize) {
        PageResult pageResult = new PageResult(data);
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        Result result = new Result();
        result.setCode(1);
        result.setData(pageResult);
        return result;
    }

    public static Result fail() {
        Result result = new Result();
        result.setCode(0);
        return result;
    }


    public static Result fail(String message) {
        Result result = new Result();
        result.setMessage(message);
        result.setCode(0);
        return result;
    }


    public static Result fail(String messageBody, Object... arg) {
        Result result = new Result();
        result.setCode(0);
        result.setMessage(StrParamUtil.into(messageBody, arg));
        return result;
    }


    protected static List<JSONObject> filterList(List<? extends Serializable> list, JSONObject jsonModel) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        Set<String> keySet = jsonModel.keySet();
        List<JSONObject> resultList = new LinkedList<>();
        for (Serializable each : list) {
            resultList.add(filterObject(each, keySet));
        }
        return resultList;
    }


    protected static JSONArray filterListToJsonArray(List<? extends Serializable> list, JSONObject jsonModel) {
        if (CollectionUtils.isEmpty(list)) {
            return new JSONArray();
        }
        Set<String> keySet = jsonModel.keySet();
        JSONArray jsonArrayResult = new JSONArray();
        for (Serializable each : list) {
            jsonArrayResult.add(filterObject(each, keySet));
        }
        return jsonArrayResult;
    }


    protected static JSONObject filterObject(Serializable one, Set<String> keySet) {
        JSONObject opt = JSONObject.from(one);
        JSONObject result = new JSONObject();
        if (CollectionUtils.isEmpty(keySet)) {
            return result;
        }
        for (String eachKey : keySet) {
            result.put(eachKey, opt.get(eachKey));
        }
        return result;
    }


    protected static JSONObject filterObject(Serializable one, JSONObject jsonModel) {
        Set<String> keySet = jsonModel.keySet();
        return filterObject(one, keySet);
    }


}